// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './vuex/store'
import request from './assets/scripts/common/ajax';
import BW from './assets/scripts/common/bwCommon'
import urls from './assets/scripts/common/apiUrls'
import axios from 'axios'
// import ElementUI from 'element-ui';
// import 'element-ui/lib/theme-chalk/index.css';
import apiReq from './assets/scripts/common/apiReq';
import filters from './assets/scripts/common/filters';
import FastClick from 'fastclick';
import VueScroller from 'vue-scroller'
import VueJsonp from 'vue-jsonp'
import jsonpRequest from './assets/scripts/common/jsonp'
// import vconsole from './assets/scripts/common/vconsole';
import Base64 from 'js-base64'
import 'babel-polyfill'
import Es6Promise from 'es6-promise'
Es6Promise.polyfill()
Vue.use(VueScroller)
Vue.use(VueJsonp)

FastClick.attach(document.body);
// Vue.use(ElementUI);
Vue.prototype.Base64 = Base64.Base64;
// Vue.prototype.vconsole = vconsole;
Vue.prototype.jsonpRequest = jsonpRequest;
Vue.prototype.request = request;
Vue.prototype.apiReq = apiReq;

Vue.config.productionTip = false

Object.keys(filters).forEach(key => Vue.filter(key, filters[key]))

// Vue.prototype.$http = axios;   // 原型上增加属性指向axios方法，任何地方都可直接使用

/* eslint-disable no-new */
// new Vue({
//     el: '#app',
//     router,
//     template: '<App/>',
//     components: { App }
// })

// 使用钩子函数对路由进行openid相关处理
router.beforeEach(async (to, from, next) => {
  const isNeedLogin = store.getters.getReLoginFlag    // 获取是否需要登录标识
  // const isNeedLogin = true
  const pageQuery = BW.getRequest()
  if (isNeedLogin) {      // 需要登录（获取openid）
    axios({
      method: 'get',
      url: urls['reqOpenId'],
      params: {code: pageQuery.code},
      responseType: 'json',
      headers: {'Content-Type': 'application/json'}
    }).then((res) => {
      if (res.data) {
        if (res.data.rspCd === '00000') {
          let result = res.data
          store.commit('setOpenId', result.data.openId)
          let usrInfo = store.getters.getUsrInfo
          if (usrInfo === null || typeof (usrInfo) === 'undefined') {
            usrInfo = {registed: result.data.registed}
          } else {
            usrInfo.registed = result.data.registed
          }
          store.commit('setUsrInfo', usrInfo)
        } else {
          BW.Toast(res.data.rspInf || '错误提示数据异常！')
        }
      }
      store.commit('updateLoginFlag', false)
      next()
    }).catch(() => {
      store.commit('updateLoginFlag', false)
      next()
    });
  } else {
    next()
  }
})
window.wVue = new Vue({
  store,
  router,
  render: h => h(App)
}).$mount('#app');
